﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Threading;

using desktop.Login;
using desktop.db;
using desktop.HelperForms;
using desktop.Igrica;
using desktop.Entity;
using desktop.Prodavac.GUI;
using desktop.Serviser.GUI;

namespace desktop
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            SplashForm.StartSplashScreen();           

            // ---------------------------
            // Inicijalizacija ide ovdje.
            // - startup applikacije
            //


            SplashForm.SetStatus("Inicijaliziram konekciju na bazu..");

            Database database = db.MySql.getInstance();
            if (!database.Success)
            {
                SplashForm.EndSplashScreen();
                MessageBox.Show(database.LastError, "Greska", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }


            // ---------------------------


            // Zatvaranje splash screen-a
            SplashForm.SetStatus("Sve uspjesno zavrseno!");
            SplashForm.EndSplashScreen();


            bool loginAgain = true;

            while(loginAgain)
            {
                LoginForm loginForm = new LoginForm(database);
                loginForm.ShowDialog();

                loginAgain = loginForm.Success;

                if (loginForm.Success)
                {
                    Form form = null;

                    switch (loginForm.LogiraniUposlenik.Tip)
                    {
                        case TipUposlenika.Prodavac:
                            form = new ProdavacForm(loginForm.LogiraniUposlenik);                            
                            break;
                        case TipUposlenika.Serviser:
                            form = new ServiserForm();
                            break;
                        case TipUposlenika.Dostavljac:                            
                            throw new Exception("NOT IMPLEMENTED!");
                            break;
                        case TipUposlenika.Menadzer:
                            throw new Exception("NOT IMPLEMENTED!");
                            break;
                    }
                    
                    if (form != null)
                        loginAgain = form.ShowDialog() == DialogResult.OK;
                }
            }
        }
    }
}
